//! 全局定义构建脚本
use std::{fs::File, io::Write, path::PathBuf};

use aarch64_define::{
    _TIF_WORK_MASK, BOOT_CPU_MODE_EL1, BOOT_CPU_MODE_EL2, BUG_BRK_IMM, CurrentEL_EL2,
    ENDIAN_SET_EL1, ENDIAN_SET_EL2, ESR_ELx_EC_BREAKPT_CUR, ESR_ELx_EC_BREAKPT_LOW,
    ESR_ELx_EC_BRK64, ESR_ELx_EC_DABT_CUR, ESR_ELx_EC_DABT_LOW, ESR_ELx_EC_FP_ASIMD,
    ESR_ELx_EC_FP_EXC64, ESR_ELx_EC_IABT_CUR, ESR_ELx_EC_IABT_LOW, ESR_ELx_EC_PC_ALIGN,
    ESR_ELx_EC_SHIFT, ESR_ELx_EC_SP_ALIGN, ESR_ELx_EC_SVC64, ESR_ELx_EC_SYS64, ESR_ELx_EC_UNKNOWN,
    ESR_ELx_EC_WFx, ID_AA64MMFR0_PARANGE_MAX, ID_AA64MMFR0_PARANGE_SHIFT, ID_AA64MMFR0_TGRAN_SHIFT,
    ID_AA64MMFR0_TGRAN_SUPPORTED, KIO_VADDR, KIO_VADDR_END, KMEM_VADDR, KMEM_VADDR_END,
    KPAGE_VADDR, KPAGE_VADDR_END, MAIR_ATTR, NO_SYSCALL, PAGE_SIZE, PGD_SHIFT, PGD_TYPE_TABLE,
    PSR_A_BIT, PSR_D_BIT, PSR_F_BIT, PSR_I_BIT, PSR_MODE_EL1h, PTRS_PER_PGD, PTRS_PER_PUD,
    PUD_SHIFT, PUD_SIZE, S_FRAME_SIZE, S_LR, S_ORIG_ADDR_LIMIT, S_ORIG_X0, S_PC, S_PSTATE, S_SP,
    S_STACKFRAME, S_SYSCALLNO, SCTLR_EL1_RES1, SCTLR_EL1_SET, SCTLR_EL2_RES1, SWAPPER_MM_IOFLAGS,
    SWAPPER_MM_NORMALFLAGS, TCR_A1, TCR_ASID16, TCR_CACHE_FLAGS, TCR_IPS_SHIFT, TCR_IRGN_WBWA,
    TCR_SMP_FLAGS, TCR_T0SZ, TCR_T0SZ_OFFSET, TCR_TBI0, TCR_TG_FLAGS, TCR_TxSZ, TCR_TxSZ_WIDTH,
    THREAD_STACK_SIZE, TIF_NEED_RESCHED, USER_DS,
};
use bsp_define::smp::{CPU_BOOT_STACK, CPU_BOOT_TASK};
use task_define::{TSK_STACK, TSK_TI_ADDR_LIMIT, TSK_TI_FLAGS, TSK_TI_PREEMPT};

fn main() {
    let out = &PathBuf::from("src/aarch64");
    let mut f = File::create(out.join("define.S")).unwrap();

    aarch64_write_define(&mut f);

    writeln!(f, "TSK_STACK = {TSK_STACK:#x}").unwrap();
    writeln!(f, "TSK_TI_ADDR_LIMIT = {TSK_TI_ADDR_LIMIT:#x}").unwrap();
    writeln!(f, "TSK_TI_FLAGS = {TSK_TI_FLAGS:#x}").unwrap();
    writeln!(f, "TSK_TI_PREEMPT = {TSK_TI_PREEMPT:#x}").unwrap();

    writeln!(f, "CPU_BOOT_STACK = {CPU_BOOT_STACK:#x}").unwrap();
    writeln!(f, "CPU_BOOT_TASK = {CPU_BOOT_TASK:#x}").unwrap();

    println!("cargo:retrun-if-changed=build.rs");
}

fn aarch64_write_define(f: &mut File) {
    writeln!(f, "KIO_VADDR = {KIO_VADDR:#x}").unwrap();
    writeln!(f, "KIO_VADDR_END = {KIO_VADDR_END:#x}").unwrap();
    writeln!(f, "KMEM_VADDR = {KMEM_VADDR:#x}").unwrap();
    writeln!(f, "KMEM_VADDR_END = {KMEM_VADDR_END:#x}").unwrap();
    writeln!(f, "KPAGE_VADDR = {KPAGE_VADDR:#x}").unwrap();
    writeln!(f, "KPAGE_VADDR_END = {KPAGE_VADDR_END:#x}").unwrap();

    writeln!(f, "CurrentEL_EL2 = {CurrentEL_EL2:#x}").unwrap();

    writeln!(f, "SCTLR_EL1_RES1 = {SCTLR_EL1_RES1:#x}").unwrap();
    writeln!(f, "SCTLR_EL2_RES1 = {SCTLR_EL2_RES1:#x}").unwrap();

    writeln!(f, "ENDIAN_SET_EL1 = {ENDIAN_SET_EL1:#x}").unwrap();
    writeln!(f, "ENDIAN_SET_EL2 = {ENDIAN_SET_EL2:#x}").unwrap();

    writeln!(f, "BOOT_CPU_MODE_EL1 = {BOOT_CPU_MODE_EL1:#x}").unwrap();
    writeln!(f, "BOOT_CPU_MODE_EL2 = {BOOT_CPU_MODE_EL2:#x}").unwrap();

    writeln!(f, "PSR_F_BIT = {PSR_F_BIT:#x}").unwrap();
    writeln!(f, "PSR_I_BIT = {PSR_I_BIT:#x}").unwrap();
    writeln!(f, "PSR_A_BIT = {PSR_A_BIT:#x}").unwrap();
    writeln!(f, "PSR_D_BIT = {PSR_D_BIT:#x}").unwrap();

    writeln!(f, "PSR_MODE_EL1h = {PSR_MODE_EL1h:#x}").unwrap();

    writeln!(f, "MAIR_ATTR = {MAIR_ATTR:#x}").unwrap();

    writeln!(f, "PGD_TYPE_TABLE = {PGD_TYPE_TABLE:#x}").unwrap();
    writeln!(f, "SWAPPER_MM_NORMALFLAGS = {SWAPPER_MM_NORMALFLAGS:#x}").unwrap();
    writeln!(f, "SWAPPER_MM_IOFLAGS = {SWAPPER_MM_IOFLAGS:#x}").unwrap();

    writeln!(f, "PGD_SHIFT = {PGD_SHIFT:#x}").unwrap();
    writeln!(f, "PUD_SHIFT = {PUD_SHIFT:#x}").unwrap();
    writeln!(f, "PTRS_PER_PGD = {PTRS_PER_PGD:#x}").unwrap();
    writeln!(f, "PUD_SIZE = {PUD_SIZE:#x}").unwrap();
    writeln!(f, "PTRS_PER_PUD = {PTRS_PER_PUD:#x}").unwrap();
    writeln!(f, "PAGE_SIZE = {PAGE_SIZE:#x}").unwrap();

    writeln!(f, "THREAD_STACK_SIZE = {THREAD_STACK_SIZE:#x}").unwrap();

    writeln!(f, "SCTLR_EL1_SET = {SCTLR_EL1_SET:#x}").unwrap();

    writeln!(f, "TCR_T0SZ_OFFSET = {TCR_T0SZ_OFFSET:#x}").unwrap();
    writeln!(f, "TCR_T0SZ = {TCR_T0SZ:#x}").unwrap();
    writeln!(f, "TCR_TxSZ = {TCR_TxSZ:#x}").unwrap();
    writeln!(f, "TCR_TxSZ_WIDTH = {TCR_TxSZ_WIDTH:#x}").unwrap();
    writeln!(f, "TCR_IRGN_WBWA = {TCR_IRGN_WBWA:#x}").unwrap();

    writeln!(f, "TCR_CACHE_FLAGS = {TCR_CACHE_FLAGS:#x}").unwrap();
    writeln!(f, "TCR_SMP_FLAGS = {TCR_SMP_FLAGS:#x}").unwrap();
    writeln!(f, "TCR_TG_FLAGS = {TCR_TG_FLAGS:#x}").unwrap();

    writeln!(f, "TCR_A1 = {TCR_A1:#x}").unwrap();
    writeln!(f, "TCR_ASID16 = {TCR_ASID16:#x}").unwrap();
    writeln!(f, "TCR_TBI0 = {TCR_TBI0:#x}").unwrap();

    writeln!(f, "ID_AA64MMFR0_PARANGE_MAX = {ID_AA64MMFR0_PARANGE_MAX:#x}").unwrap();
    writeln!(f, "ID_AA64MMFR0_PARANGE_SHIFT = {ID_AA64MMFR0_PARANGE_SHIFT:#x}").unwrap();
    writeln!(f, "TCR_IPS_SHIFT = {TCR_IPS_SHIFT:#x}").unwrap();

    writeln!(f, "ID_AA64MMFR0_TGRAN_SHIFT = {ID_AA64MMFR0_TGRAN_SHIFT:#x}").unwrap();
    writeln!(f, "ID_AA64MMFR0_TGRAN_SUPPORTED = {ID_AA64MMFR0_TGRAN_SUPPORTED:#x}").unwrap();

    writeln!(f, "USER_DS = {USER_DS:#x}").unwrap();

    writeln!(f, "S_LR = {S_LR:#x}").unwrap();
    writeln!(f, "S_SP = {S_SP:#x}").unwrap();
    writeln!(f, "S_PC = {S_PC:#x}").unwrap();
    writeln!(f, "S_PSTATE = {S_PSTATE:#x}").unwrap();
    writeln!(f, "S_ORIG_X0 = {S_ORIG_X0:#x}").unwrap();
    writeln!(f, "S_ORIG_ADDR_LIMIT = {S_ORIG_ADDR_LIMIT:#x}").unwrap();
    writeln!(f, "S_STACKFRAME = {S_STACKFRAME:#x}").unwrap();
    writeln!(f, "S_SYSCALLNO = {S_SYSCALLNO:#x}").unwrap();
    writeln!(f, "S_FRAME_SIZE = {S_FRAME_SIZE:#x}").unwrap();

    writeln!(f, "NO_SYSCALL = {NO_SYSCALL:#x}").unwrap();

    writeln!(f, "BUG_BRK_IMM = {BUG_BRK_IMM:#x}").unwrap();

    writeln!(f, "ESR_ELx_EC_SHIFT = {ESR_ELx_EC_SHIFT:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_UNKNOWN = {ESR_ELx_EC_UNKNOWN:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_WFx = {ESR_ELx_EC_WFx:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_FP_ASIMD = {ESR_ELx_EC_FP_ASIMD:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_SVC64 = {ESR_ELx_EC_SVC64:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_SYS64 = {ESR_ELx_EC_SYS64:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_SVE = {ESR_ELx_EC_SVC64:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_IABT_LOW = {ESR_ELx_EC_IABT_LOW:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_IABT_CUR = {ESR_ELx_EC_IABT_CUR:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_PC_ALIGN = {ESR_ELx_EC_PC_ALIGN:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_DABT_LOW = {ESR_ELx_EC_DABT_LOW:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_DABT_CUR = {ESR_ELx_EC_DABT_CUR:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_SP_ALIGN = {ESR_ELx_EC_SP_ALIGN:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_FP_EXC64 = {ESR_ELx_EC_FP_EXC64:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_BREAKPT_LOW = {ESR_ELx_EC_BREAKPT_LOW:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_BREAKPT_CUR = {ESR_ELx_EC_BREAKPT_CUR:#x}").unwrap();
    writeln!(f, "ESR_ELx_EC_BRK64 = {ESR_ELx_EC_BRK64:#x}").unwrap();

    writeln!(f, "TIF_NEED_RESCHED = {TIF_NEED_RESCHED:#x}").unwrap();
    writeln!(f, "_TIF_WORK_MASK = {_TIF_WORK_MASK:#x}").unwrap();
}
